package com.mgface.eth.api;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Convert;

/**
 * @author wanyuxiang
 * @version 1.0
 * @project eth
 * @desc 获取账户的余额
 * @create 2018-06-26 22:07
 **/
public class GetBanlance {

	public static void  ethGetBalance(String address) throws Exception {
		Web3j web3j = Web3j.build(new HttpService("https://ropsten.infura.io/IY77yuopq4bSgHoaFqJe"));
		EthGetBalance balance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();
		if(balance!=null){
			// 打印账户余额
			System.out.println(balance.getBalance());
			// 将单位转为以太，方便查看
			System.out.println(Convert.fromWei(balance.getBalance().toString(), Convert.Unit.ETHER));
		}
	}

	public static void main(String[] args)  throws Exception {
		ethGetBalance("0x44b0574bd2d4c5cbe9b55d70a95e3b3ad41490cb");
	}
}
